#!/bin/sh
# http://marius.scurtescu.com/

# Licence : GNU GPL

# Dependency
#		mogrify (in imagemagick)
#		zenity
# based on
#       ImageTool cteated by Chris Wilson
#       http://blixtra.org/blog/installing-nautilus-scripts/

TITLE="Conver Raw Image(s)"

if [ $# == 0 ] ; then
  zenity --error --title="$TITLE" --text="Please select at least one image."
  exit 2
fi

which dcraw 2>/dev/null
if [ $? != 0 ] ; then
	zenity --error --title="$TITLE" --text="dcraw not found, please install it."
	exit 1
fi

progress=0
counter=1

(for i in "$@"
  ##echoed strings starting with "#" are used as text in the the progress window
  ##echoed integers are used as the percentage for the progress bar
  do
    echo $progress
    
    echo "#Converting image `basename \"${i}\"` ($counter/$#)"
    
    o = `echo $i | sed "s/\.cr2$/.jpg/"`
    dcraw -c "${i}" | cjpeg -outfile "${o}"
    
    ((progress = $counter*100/$#)) 
    ((counter+=1))
  done
  echo 100
  echo "#Done") | zenity --progress --title="$TITLE" --text="Converting Image ..." --percentage=${progress}

exit 0
